<template>
	<view>
		<view class="haed-tabs flex">
			<text class="flex1" :class="[tabsIndex==0?'active':'']" @click="tabs(0)">我的求购</text>
			<text class="flex1" :class="[tabsIndex==1?'active':'']" @click="tabs(1)">我的回复</text>
		</view>
		<view class="main" v-if="buyList.length>0">
			<view class="my-buy" v-if="tabsIndex==0">
				<myBuy :list="buyList"></myBuy>
			</view>
			<view class="my-replay" v-else>
				<myBuy :list="buyList"></myBuy>
			</view>
		</view>
		<view class="no-data" v-else>
			<image src="../../static/img/noData.png" mode=""></image>
			<view>暂无数据，<navigator open-type="switchTab" url="/pages/buy/index">快去求购吧</navigator>
			</view>
		</view>
		<view class="loading flex flai fljc" v-if="loading">
			<image src="../../static/img/loading2.gif" mode="" v-if="loadPic"></image>
			<text>{{msg}}</text>
		</view>
		<view style="height: 60rpx;"></view>
	</view>
</template>

<script>
	import myBuy from '../components/myBuy/index.vue'
	import API from '../../api/index.js'
	export default {
		components: {
			myBuy
		},
		data() {
			return {
				tabsIndex: 0,
				page: 1,
				buyList: [],
				type:1,
				isPull:true
			}
		},
		onLoad() {
			console.log('进入')
			this.getList()
		},
		methods: {
			tabs(index) {
				this.tabsIndex = index
				this.type = index==0?1:2
				this.buyList = []
				this.getList()
			},
			
			getList() {
				var data = {
					page: this.page,
					size: 10,
					type: this.type
				}
				this.loading = true
				this.msg='加载中...'
				this.loadPic = true
				API.indexHome.myBuySigle(data).then(res => {
					if (res.errno == '0') {
						var needs = res.data
						if (needs.length < 10 && needs.length>0) {
							this.isPull = false
							this.msg ='主人我是有底线的哟~~'
							this.loadPic = false
						} else {
							this.isPull = true
							setTimeout(()=>{
								this.loading = false
							},1000)
						}
						this.buyList = [...this.buyList, ...needs]
					}
				})
			}
		},
		onPullDownRefresh() {
			this.page = 1
			this.buyList = []
			this.getList()
			setTimeout(function() {
				uni.stopPullDownRefresh();
			}, 1000);
		},
		onReachBottom() {
			if (this.isPull) {
				var page = this.page + 1
				console.log(page)
				this.page = page
				this.getList()
			}
			
		}
	}
</script>

<style>
	@import url("./index.css");
</style>
